/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.admin.model;

import com.common.json.JsonModel;
import com.entities.ContactMessage;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.StoredProcedureQuery;

/**
 *
 * @author Tuan Long
 */
@Stateless
public class ContactMessageFacade extends AbstractFacade<ContactMessage> implements ContactMessageFacadeLocal {

    @PersistenceContext(unitName = "BluePumpkinEJBPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ContactMessageFacade() {
        super(ContactMessage.class);
    }

    @Override
    public List<String> getNewMessage() {
        List<String> listJson = new ArrayList<String>();
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("ContactMessage.getNewMessage");
        List<Object[]> results = query.getResultList();
        for (Object[] row : results) {
            int id = (int) row[0];
            String sender = (String) row[1];
            String content = (String) row[2];
            boolean recieved = (boolean) row[3];
            Date sent = (Date) row[4];
            String json = JsonModel.getNewMessageSender(id, sender, content, recieved, sent);
            listJson.add(json);
        }
        return listJson;
    }

    @Override
    public List<String> activeMessageRead(int msgId) {
        List<String> listJson = new ArrayList<String>();
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("ContactMessage.activeMessageRead");
        StoredProcedureQuery update = em.createNamedStoredProcedureQuery("ContactMessage.updateRecievedMessageRead");
        update.setParameter("Id", msgId);
        query.setParameter("Id", msgId);
        int result = update.executeUpdate();
        if (result > 0) {
            List<Object[]> results = query.getResultList();
            for (Object[] row : results) {
                int id = (int) row[0];                
                String sender = (String) row[1];                
                String content = (String) row[2];                
                boolean recieved = (boolean) row[3];                
                Date sent = (Date) row[4];                
                String json = JsonModel.getNewMessageSender(id, sender, content, recieved, sent);
                listJson.add(json);
            }
        }
        return listJson;
    }

    @Override
    public int getNewRegis() {
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("ContactMessage.getNewNotificationRegistration");
        Object[] result = (Object[]) query.getSingleResult();
        int count;
        count = (int)result[0];
        return count;
    }

    @Override
    public int getNewComments() {
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("ContactMessage.getNewNotificationComment");
        Object[] result = (Object[]) query.getSingleResult();
        int count;
        count = (int)result[0];
        return count;
    }

}
